package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PageResult;
import com.itheima.common.ResultData;
import com.itheima.domain.SmsFlashPromotion;
import com.itheima.domain.SmsFlashPromotionSession;
import com.itheima.service.SmsFlashPromotionService;
import com.itheima.service.SmsFlashPromotionSessionService;
import com.itheima.vo.SessionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Morty
 * @create 2022 11 11 17:40
 */
@RestController
@CrossOrigin(origins = "*")
//秒杀活动
public class SmsFlashPromotionController {
    @DubboReference
    SmsFlashPromotionService smsFlashPromotionService;
    @DubboReference
    SmsFlashPromotionSessionService smsFlashPromotionSessionService;

    //获取活动列表
    @GetMapping("/flash/list")
    public ResultData page(@RequestParam(required = false) String keyword, Integer pageNum, Integer pageSize) {
        Page<SmsFlashPromotion> page = smsFlashPromotionService.page(keyword, pageNum, pageSize);
        List<SmsFlashPromotion> list = page.getRecords();
        long total = page.getTotal();
        long pages = page.getPages();
        return ResultData.ok(new PageResult(pageNum, pageSize, total, pages, list), "");
    }

    //新增活动
    @PostMapping("/flash/create")
    public ResultData save(@RequestBody SmsFlashPromotion flashPromotion) {
        smsFlashPromotionService.save(flashPromotion);
        return ResultData.ok("", "");
    }

    //删除活动
    @PostMapping("/flash/delete/{id}")
    public ResultData deleteById(@PathVariable Integer id) {
        smsFlashPromotionService.removeById(id);
        return ResultData.ok("", "");
    }

    //修改上下线状态
    @PostMapping("/flash/update/status/{id}")
    public ResultData updateStatus(@PathVariable Integer id, Integer status) {
        SmsFlashPromotion flashPromotion = smsFlashPromotionService.getById(id);
        flashPromotion.setStatus(status);
        smsFlashPromotionService.updateById(flashPromotion);
        return ResultData.ok("", "");
    }
    //修改活动
    @PostMapping("/flash/update/{id}")
    public ResultData findById(@RequestBody  SmsFlashPromotion smsFlashPromotion,@PathVariable Integer id){
        smsFlashPromotion.setId(Long.valueOf(id));
        smsFlashPromotionService.updateById(smsFlashPromotion);
        return ResultData.ok("","");
    }
    //获取全部活动场次
    @GetMapping("/flashSession/list")
    public ResultData list(){
        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionService.list();
        return ResultData.ok(list,"");
    }
    //添加场次
    @PostMapping("/flashSession/create")
    public ResultData saveSession(@RequestBody SmsFlashPromotionSession smsFlashPromotionSession){
        smsFlashPromotionSessionService.save(smsFlashPromotionSession);
        return ResultData.ok("","");
    }
    //删除场次
    @PostMapping("/flashSession/delete/{id}")
    public ResultData deleteSessionById(@PathVariable Integer id){
        smsFlashPromotionSessionService.removeById(id);
        return ResultData.ok("","");
    }
    //获取全部可选场次及其数量
    @GetMapping("/flashSession/selectList")
    public ResultData selectById(Integer flashPromotionId){
        List<SessionVo> list = smsFlashPromotionSessionService.selectListByid(flashPromotionId);
        return ResultData.ok(list,"");
    }
    //修改场次
    @PostMapping("/flashSession/update/{id}")
    public ResultData updateSession(@RequestBody SmsFlashPromotionSession flashPromotionSession ,@PathVariable Integer id){
        smsFlashPromotionSessionService.updateById(flashPromotionSession);
        return ResultData.ok("","'");
    }
    //修改场次状态
    @PostMapping("/flashSession/update/status/{id}")
    public ResultData updateSessionStatus(@PathVariable Integer id,Integer status){
        SmsFlashPromotionSession flashPromotionSession = smsFlashPromotionSessionService.getById(id);
        flashPromotionSession.setStatus(status);
        smsFlashPromotionSessionService.updateById(flashPromotionSession);
        return ResultData.ok("","");

    }
}
